/*******************************************************************************
 * Copyright 2014 Dean Ding.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.handmark.pulltorefresh.library;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * Support RecyclerView
 *
 * @author Dean.Ding
 */
public class PullToRefreshRecyclerView extends PullToRefreshBase<RecyclerView> {

    public PullToRefreshRecyclerView(Context context) {
        super(context);
        mRefreshableView.setOnScrollListener(mOnScrollListener);
    }

    public PullToRefreshRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRefreshableView.setOnScrollListener(mOnScrollListener);
    }

    public PullToRefreshRecyclerView(Context context, Mode mode) {
        super(context, mode);
        mRefreshableView.setOnScrollListener(mOnScrollListener);
    }

    public PullToRefreshRecyclerView(Context context, Mode mode, AnimationStyle style) {
        super(context, mode, style);
        mRefreshableView.setOnScrollListener(mOnScrollListener);
    }

    private OnLastItemVisibleListener mOnLastItemVisibleListener;

    @Override
    public final Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        RecyclerView recyclerView;
        recyclerView = new RecyclerView(context, attrs);
        recyclerView.setId(R.id.recyclerview);
//        addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
        return recyclerView;
    }

    @Override
    protected boolean isReadyForPullStart() {
        if (mRefreshableView.getChildCount() <= 0) {
            return true;
        }
        int firstVisiblePosition =
                mRefreshableView.getChildPosition(mRefreshableView.getChildAt(0));
        if (firstVisiblePosition == 0) {
//            return (mRefreshableView.getChildAt(0).getTop() - mRefreshableView.getPaddingTop() - mRefreshableView.getChildAt(0).getPaddingTop()) == 0;
            return mRefreshableView.getChildAt(0).getTop() >= mRefreshableView.getPaddingTop();
        } else {
            return false;
        }

    }

    @Override
    protected boolean isReadyForPullEnd() {
        int lastVisiblePosition = mRefreshableView
                .getChildPosition(mRefreshableView.getChildAt(mRefreshableView.getChildCount() - 1));
        if (mRefreshableView.getChildAt(mRefreshableView.getChildCount() - 1) != null && lastVisiblePosition >= mRefreshableView.getAdapter().getItemCount() - 1) {
            return mRefreshableView.getChildAt(mRefreshableView.getChildCount() - 1).getBottom()
                    <= mRefreshableView.getBottom();
        }
        return false;
    }

    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (mOnLastItemVisibleListener != null && isReadyForPullEnd()) {
                mOnLastItemVisibleListener.onLastItemVisible();
            }
        }
    };

    public final void setOnLastItemVisibleListener(OnLastItemVisibleListener listener) {
        mOnLastItemVisibleListener = listener;
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        mRefreshableView.setAdapter(adapter);
    }

    public void setLayoutManager(RecyclerView.LayoutManager layout) {
        mRefreshableView.setLayoutManager(layout);
    }

    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        mRefreshableView.addItemDecoration(itemDecoration);
    }
}
